package com.qiang.testspecialview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

public class RegularHexagonView extends TextView{
    private static final String TAG = "RegularHexagonView";
    
    public static final int TEXT_ALIGN_LEFT              = 0x00000001;
    public static final int TEXT_ALIGN_RIGHT             = 0x00000010;
    public static final int TEXT_ALIGN_CENTER_VERTICAL   = 0x00000100;
    public static final int TEXT_ALIGN_CENTER_HORIZONTAL = 0x00001000;
    public static final int TEXT_ALIGN_TOP               = 0x00010000;
    public static final int TEXT_ALIGN_BOTTOM            = 0x00100000;

    /**当文本不是在正中间而是在顶部,左边,右边,底部时文件与控件边界的距离.*/
    private static final int PADDING_LEFT = 20;
    private static final int PADDING_RIGHT = 20;
    private static final int PADDING_TOP = 20;
    private static final int PADDING_BOTTOM = 40;


    /** 控件画笔 */
    private Paint paint;
    /** 文字的方位(默认情况下居中显示) */
    private int textAlign=TEXT_ALIGN_CENTER_HORIZONTAL | TEXT_ALIGN_BOTTOM;
    /** 文字的颜色 */
    private int textColor;
    /**文字大小*/
    private float textSize;;
    /**文本内容的宽度*/
    private float txtWidth;
    /** 控件的宽度 */
    private int viewWidth;
    /** 控件的高度 */
    private int viewHeight;
    /** 文本中轴线X坐标 */
    private float textCenterX;
    /** 文本baseline线Y坐标 */
    private float textBaselineY;

    /**从xml布局解析出来的文本*/
    private String text;

    /**真正绘制的文本*/
    private String drawText;

    private FontMetrics fm;

    private Context mContext;
    private boolean selected = false;

    /**控件的编号,默认值0表示正中间的正六边形.*/
    private int index=0;

    public RegularHexagonView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public RegularHexagonView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init();
    }

    public RegularHexagonView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;

        //读取自定义属性.
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.regularHexagonView);
        index = typedArray.getInt(R.styleable.regularHexagonView_index,0);
        setIndex(index);

        init();
    }

    /**
     * 变量初始化
     */
    private void init() {
        text = getText().toString();
        drawText = text;
        //这一步必须有,目的是将xml解析出来的文本清空,因为我们在onDraw()里即将重新绘制文本.如果不清空,将重复绘制,
        setText("");
        textColor = getCurrentTextColor();
        textSize = getTextSize();

        paint = new Paint();
        paint.setTextSize(textSize);
        paint.setColor(textColor);
        paint.setAntiAlias(true);
    }


    public void setDrawText(String text){
        this.drawText = text;
        invalidate();
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getIndex() {
        return index;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int wMode = MeasureSpec.getMode(widthMeasureSpec);
        int wSize = MeasureSpec.getSize(widthMeasureSpec);

        int hMode = MeasureSpec.getMode(heightMeasureSpec);
        int hSize = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(wSize, hSize);
        Log.i(TAG, "onMeasure()--wMode=" + wMode + ",wSize=" + wSize + ",hMode=" + hMode+ ",hSize=" + hSize);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
            int bottom) {
        Log.i(TAG, "onLayout");
        viewWidth = right - left; 
        viewHeight = bottom - top;
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //之所以不去掉这一行,因为除了文本外,还有很多其他内容,比如drawableTop小图片等需要通过它来绘制,
        //正因为如此,所以我们才在init()里setText(""),目的就是为了除了文本我们自己绘制,其他一律父类Textiew来绘制.
        super.onDraw(canvas);

        //根据对其方式确定文本位置.
        setTextLocation(drawText);

        //先//获取文本内容的宽度,然后绘制文本.
        txtWidth = paint.measureText(drawText);
        canvas.drawText(drawText, textCenterX-txtWidth/2, textBaselineY, paint);
    }

    /**
     * 定位文本绘制的位置
     */
    private void setTextLocation(String text) {
        fm = paint.getFontMetrics();
        //文本的宽度
        float textWidth = paint.measureText(text);
        float textCenterVerticalBaselineY = viewHeight / 2 - fm.descent + (fm.descent - fm.ascent) / 2;
        switch (textAlign) {
        case TEXT_ALIGN_CENTER_HORIZONTAL | TEXT_ALIGN_CENTER_VERTICAL:
            textCenterX = (float)viewWidth / 2;
            textBaselineY = textCenterVerticalBaselineY;
            break;
        case TEXT_ALIGN_LEFT | TEXT_ALIGN_CENTER_VERTICAL:
            textCenterX = textWidth / 2+PADDING_LEFT;
            textBaselineY = textCenterVerticalBaselineY;
            break;
        case TEXT_ALIGN_RIGHT | TEXT_ALIGN_CENTER_VERTICAL:
            textCenterX = viewWidth - textWidth / 2-PADDING_RIGHT;
            textBaselineY = textCenterVerticalBaselineY;
            break;
        case TEXT_ALIGN_BOTTOM | TEXT_ALIGN_CENTER_HORIZONTAL:
            textCenterX = viewWidth / 2;
            textBaselineY = viewHeight - fm.bottom-PADDING_BOTTOM;
            break;
        case TEXT_ALIGN_TOP | TEXT_ALIGN_CENTER_HORIZONTAL:
            textCenterX = viewWidth / 2;
            textBaselineY = -fm.ascent+PADDING_TOP;
            break;
        case TEXT_ALIGN_TOP | TEXT_ALIGN_LEFT:
            textCenterX = textWidth / 2+PADDING_TOP;
            textBaselineY = -fm.ascent+PADDING_LEFT;
            break;
        case TEXT_ALIGN_BOTTOM | TEXT_ALIGN_LEFT:
            textCenterX = textWidth / 2+PADDING_LEFT;
            textBaselineY = viewHeight - fm.bottom-PADDING_BOTTOM;
            break;
        case TEXT_ALIGN_TOP | TEXT_ALIGN_RIGHT:
            textCenterX = viewWidth - textWidth / 2+PADDING_TOP;
            textBaselineY = -fm.ascent-PADDING_RIGHT;
            break;
        case TEXT_ALIGN_BOTTOM | TEXT_ALIGN_RIGHT:
            textCenterX = viewWidth - textWidth / 2-PADDING_RIGHT;
            textBaselineY = viewHeight - fm.bottom-PADDING_BOTTOM;
            break;
        }
    }

}
